# *_*coding:utf-8 *_*
# @Author : Reggie
# @Time : 2021/2/8 下午 1:44
import time
from pathlib import Path
from urllib.parse import urljoin

from dataclasses import dataclass

from logger import logger
from base_m3u8 import DownLoad_M3U8


@dataclass
class Porn91(DownLoad_M3U8):
    def get_ts_url(self):
        m3u8_obj = self.m3u8_obj
        # base_uri = m3u8_obj.base_uri
        base_uri = self.m3u8_url[:self.m3u8_url[:self.m3u8_url.find("?")].rfind("/")]
        total = len(m3u8_obj.segments)
        logger.info("Total: {}".format(total))
        for seg in m3u8_obj.segments:
            # uri = urljoin(base_uri, seg.uri)
            uri = f"{base_uri}/{seg.uri}"
            # print(uri)
            # uri = seg.absolute_uri
            yield uri

    def download_all_ts(self):
        self.download_all_ts2()


if __name__ == '__main__':
    # m3u8_url = 'https://newv1.xinliangfc.com/01yxkcGuyG.m3u8'
    # 使用系统变量修改代理, 可以让m3u8解析也经过代理
    # os.environ["http_proxy"] = "http://127.0.0.1:10809"
    # os.environ["https_proxy"] = "https://127.0.0.1:10809"
    # print(os.environ)
    m3u8_url = "https://cdn.91p07.com//m3u8/432400/432400.m3u8?st=n2WbuHS0m6x070CMYFK1HA&e=1612771280&f=7675FaEDUop0dyTskGOUmelERXOddblzoPhRGd1XOgMIr8fRypOp7DUsj1mWcA3yNWXp8Qst+CKTQeok34wXSjZIYfDNkswcGw7yQvA"
    file_name = '怒操小骚货背着男友出来偷情'
    ts_path = "../tmp"
    out_path = Path(r"C:\download").joinpath(Path(__file__).stem)
    proxies = {
        'sock5': '127.0.0.1:10808',
        'http': '127.0.0.1:10809',
        'https': '127.0.0.1:10809'
    }
    # proxies = {}
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36'
    }
    download_proxies = proxies
    download_headers = {
        "Referer": "https://twitter.com/home",
        "origin": "https://twitter.com",
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 dingtalk-win/1.0.0 nw(0.14.7) DingTalk(4.6.39-Release.10) Mojo/1.0.0 Native AppType(release)',
    }

    start = time.time()
    M3U8 = Porn91(
        m3u8_url=m3u8_url,
        file_name=file_name,
        out_path=out_path,
        ts_path=ts_path,
        proxies=proxies,
        rm_ts=False,
        headers=headers,
        max_workers=1,
        download_headers=download_headers,
        download_proxies=download_proxies
    )
    M3U8.run()
    end = time.time()
    print('耗时:', end - start)
